package com.xxliang.yunc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xxliang.yunc.api.OrderApi;
import com.xxliang.yunc.domain.*;
import com.xxliang.yunc.exception.GlobalBussnessException;
import com.xxliang.yunc.mapper.PayOrderMapper;
import com.xxliang.yunc.service.IAlipayInfoService;
import com.xxliang.yunc.service.IPayFlowService;
import com.xxliang.yunc.service.IPayOrderService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xxliang.yunc.util.AssertUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxliang
 * @since 2025-06-22
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Autowired
    private IAlipayInfoService alipayInfoService;
    @Autowired
    private IPayOrderService iPayOrderService;
    @Autowired
    private IPayFlowService iPayFlowService;
    @Autowired
    private OrderApi orderApi;

    @Override
    public void payNotify(AlipayNotifyDto alipayNotifyDto) {
        String orderNo = alipayNotifyDto.getOut_trade_no();
        PayOrder payOrder = selectOne(new EntityWrapper<PayOrder>().eq("order_no", orderNo));
        AssertUtil.isNull(payOrder,"订单不存在");


        //验签

        String jsonString = JSONObject.toJSONString(alipayNotifyDto);
        Map map = JSONObject.parseObject(jsonString, Map.class);
        try {
//          异步调用验签工作
            Boolean b = Factory.Payment.Common().verifyNotify(map);

            if(!b){
                log.info("验签失败");
                throw new GlobalBussnessException("验签失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        /*
            a. 商家需要验证该通知数据中的 out_trade_no 是否为商家系统中创建的订单号。
            b. 判断 total_amount 是否确实为该订单的实际金额（即商户订单创建时的金额）。
            c. 校验通知中的 seller_id（或者 seller_email) 是否为 out_trade_no 这笔单据的对应的操作方（有的时候，一个商家可能有多个 seller_id/seller_email）。
            d. 验证 app_id 是否为该商家本身。
        */

        /*
          更新表：t_pay_order
         */
        if (alipayNotifyDto.isTradeSuccess()){
            payOrder.setPayStatus(PayOrder.PAY_STATUS_PAID);
            iPayOrderService.updateById(payOrder);

            log.info("订单支付成功");
        }else {
            payOrder.setPayStatus(PayOrder.PAY_STATUS_CANCEL);
            iPayOrderService.updateById(payOrder);
            log.info("订单支付失败");
        }
        //添加流水
        PayFlow payFlow = new PayFlow();
        // String - > 时间
        Date date = new Date();
        payFlow.setNotifyTime(date);
        payFlow.setSubject(alipayNotifyDto.getSubject());
        payFlow.setOutTradeNo(orderNo);
        payFlow.setTotalAmount(new BigDecimal(alipayNotifyDto.getTotal_amount()));
        payFlow.setTradeStatus(alipayNotifyDto.getTrade_status());
        payFlow.setPaySuccess(true);
        payFlow.setPassbackParams(payOrder.getExtParams());
        iPayFlowService.insert(payFlow);
        log.info("添加流水成功");


        //更新订单信息；
        orderApi.UpdateById(orderNo);


        //更新用户的资源,保证用户的资源是同步可用的。
        log.info("更新用户资源成功");

    }

    @Override
    public void checkPayOrder(String orderId) {
        PayOrder payOrder = selectOne(new EntityWrapper<PayOrder>().eq("order_no", orderId));
        AssertUtil.isNull(payOrder,"订单不存在");
    }

    /**
     *
     * @param payApplyDto
     */
    @Override
    public String apply(PayApplyDto payApplyDto) {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        // 2. 准备请求参数
        PayOrder payOrder = iPayOrderService.selectOne(new EntityWrapper<PayOrder>().eq("order_no", payApplyDto.getOrderNo()));
        if (payOrder.getPayStatus().equals(PayOrder.PAY_STATUS_PAID)){
            throw new GlobalBussnessException("订单已支付");
        }
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePagePayResponse response = Factory.Payment.Page().pay(payOrder.getSubject(), payApplyDto.getOrderNo(),payOrder.getAmount().toString() , "blog.xxliang.top");
            // 3. 处理响应或异常
            System.out.println(response.getBody());
            if (ResponseChecker.success(response)) {
                log.info(response.toString());
//                payOrder.setPayStatus(PayOrder.PAY_STATUS_PAID);
//                iPayOrderService.updateById(payOrder);
                System.out.println("调用成功");
                return response.getBody();
            } else {
//                payOrder.setPayStatus(PayOrder.PAY_STATUS_CANCEL);
//                iPayOrderService.updateById(payOrder);
                System.err.println("调用失败，原因：" + response.getBody() + "，");
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return null;
    }

    private Config getOptions() {
        AlipayInfo alipayInfo = alipayInfoService.selectOne(new EntityWrapper<AlipayInfo>().eq("id", 1));
        Config config = new Config();
        config.protocol = "https";//协议
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";//网关地址
        config.signType = "RSA2";//签名方式
        config.appId = "9021000149683306";//""<-- 请填写您的AppId，例如：2019091767145019 -->";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCxlkuNFH0FspyE02thHhsoudTQnAKZ0FSoHkqv6YfziPHdDXi55NNJmr930iJsPOF1yYP0CD6+3UYyynD5WsGNobmTt7L1agqFB7fb8IGm66lLL25+HIfl0zlLA/mAAySARzopkIP6LTlDOWGwdq94dVbjHIBsLTge2nJjLsGt2j51pFoTqs4zAb5Cori2Gf75RG/6bYrowxhF4CObQ9LPXpVGLhRB7RUHq3N8TRLuVR3QL1bc7Eaco8h1C+M9wj1kMhTbEEhGdUcRZawKDTgpYiMJvyb/+bBEQ5tAc2W1ApofSWZeSwS+33q/UpZe6Ox/cx/9cvQ83irkDFJGutLbAgMBAAECggEBAKkueM1vvmeGMGyuCbdc+hzDZQZU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4x62Zv3UrMxCLEXjqr0C3nH9DyJ1E//CzpkPasn67q1oPxkD94U+IZWG3S95+YHH3I16x71jL+eVS2AxjnG1PCIiuCiHOkb8E+66/yfLIlMXnodjTcOorAEL0fhs=";//"<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
        //config.merchantCertPath = "";//"<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
        //config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
        //config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsZZLjRR9BbKchNNrYR4bKLnU0JwCmdBUqB5Kr+mH84jx3Q14ueTTSZq/d9IibDzhdcmD9Ag+vt1GMspw+VrBjaG5k7ey9WoKhQe32/CBpuupSy9ufhyH5dM5SwP5gAMkgEc6KZCD+i05QzlhsHaveHVW4xyAbC04HtpyYy7Brdo+daRaE6rOMwG+QqK4thn++URv+m2K6MMYReAjm0PSz16VRi4UQe0VB6tzfE0S7lUd0C9W3OxGnKPIdQvjPcI9ZDIU2xBIRnVHEWWsCg04KWIjCb8m//mwREObQHNltQKaH0lmXksEvt96v1KWXujsf3Mf/XL0PN4q5AxSRrrS2wIDAQAB";//"<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = alipayInfo.getNotifyUrl();//<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        //config.encryptKey ="";
        return config;
    }
}

